/*
*          iMaple by KururuLABO
*/

package iMaple.Packet;

import client.MapleCharacter;
import client.MapleCharacterUtil;
import client.MapleClient;
import client.inventory.Item;
import client.inventory.MapleInventory;
import client.inventory.MapleInventoryType;
import handling.login.LoginInformationProvider;
import server.MapleItemInformationProvider;
import tools.data.LittleEndianAccessor;
import tools.packet.LoginPacket;

public class CreateChar {
    public static final void CreateChar(final LittleEndianAccessor slea, final MapleClient c) {
        System.out.println("SLEA :"+slea);
        String name = slea.readMapleAsciiString();
        
        slea.readInt(); //Unknow = -1 FF FF FF FF [ตอนรับ]
        final LoginInformationProvider.JobType Job = LoginInformationProvider.JobType.getByType(slea.readInt());
        final short db = slea.readShort(); //whether dual blade = 1 or adventurer = 0
        final byte gender = slea.readByte(); //??idk corresponds with the thing in addCharStats
        byte skinColor = slea.readByte(); // 01
        slea.readByte(); // unk
        final int face = slea.readInt();
        final int hair = slea.readInt();
        
        final boolean ultimateAdventure = (Job == LoginInformationProvider.JobType.UltimateAdventurer);
        final boolean Resistance = (Job == LoginInformationProvider.JobType.Resistance);
        final boolean Adventure = (Job == LoginInformationProvider.JobType.Adventurer);
        final boolean Cygnus = (Job == LoginInformationProvider.JobType.Cygnus);
        final boolean Aran = (Job == LoginInformationProvider.JobType.Aran);
        final boolean Evan = (Job == LoginInformationProvider.JobType.Evan);
        final boolean Mercedes = (Job == LoginInformationProvider.JobType.Mercedes);
        final boolean Demon = (Job == LoginInformationProvider.JobType.Demon);
        final boolean Phantom = (Job == LoginInformationProvider.JobType.Phantom);
        final boolean Dualblade = (Job == LoginInformationProvider.JobType.DualBlade);
        final boolean Mihile = (Job == LoginInformationProvider.JobType.Mihile);
        final boolean Luminous = (Job == LoginInformationProvider.JobType.Luminous);
        final boolean Kaiser = (Job == LoginInformationProvider.JobType.Kaiser);
        final boolean AngelicBurster = (Job == LoginInformationProvider.JobType.AngelicBurster);
        final boolean CannonShooter = (Job == LoginInformationProvider.JobType.Cannonshooter);
        final boolean Xenon = (Job == LoginInformationProvider.JobType.Xenon);
        final boolean Zero = (Job == LoginInformationProvider.JobType.Zero);
        final boolean Zen = (Job == LoginInformationProvider.JobType.Zen);
        final boolean Hayato = (Job == LoginInformationProvider.JobType.Hayato);
        final boolean Kanna = (Job == LoginInformationProvider.JobType.Kanna);
        
        final boolean MarkCheck = (Job == LoginInformationProvider.JobType.Demon ||Job == LoginInformationProvider.JobType.Xenon);
        int hairColor = 0;
        if (!Mercedes && !Demon && !Phantom && !Xenon && !Mihile && !Dualblade && !Luminous && !Kaiser && !AngelicBurster) { //mercedes/demon dont need hair color since its already in the hair
            hairColor = slea.readInt();
            skinColor = (byte) slea.readInt();
        }
   
        final int Mark = MarkCheck ? slea.readInt() : 0;
        final int top = slea.readInt();
        final int bottom = (Mercedes || Demon || Phantom || Kaiser || AngelicBurster || Xenon || Zero || Hayato || Kanna ) ? 0 : slea.readInt();
        final int shoes = slea.readInt();
        final int weapon = slea.readInt();
        
        int shield = Job == LoginInformationProvider.JobType.Phantom ? 1352100 : Mercedes ? 1352000 : Demon ? slea.readInt() : 0;   
        MapleCharacter newchar = MapleCharacter.getDefault(c, Job);
        newchar.setWorld((byte) c.getWorld());
        newchar.setFace(face);
        newchar.setHair(hair + hairColor);
        newchar.setGender(gender);
        newchar.setName(name);
        newchar.setSkinColor(skinColor);
        newchar.setDemonMarking(Mark);
        
        final MapleItemInformationProvider li = MapleItemInformationProvider.getInstance();
        final MapleInventory equip = newchar.getInventory(MapleInventoryType.EQUIPPED);

        Item item = li.getEquipById(top);
        item.setPosition((byte) -5);
        equip.addFromDB(item);
        
        if (bottom > 0) { //resistance have overall
            item = li.getEquipById(bottom);
            //-1 Hat | -2 Face | -3 Eye acc | -4 Ear acc | -5 Overall | -7 Shoes | -9 Cape | -11 Weapon
            item.setPosition(Phantom ? -5 : (byte)-6);
            equip.addFromDB(item);
        }

        item = li.getEquipById(shoes);
        item.setPosition(Phantom ? -9 : (byte) -7);
        equip.addFromDB(item);

        item = li.getEquipById(weapon);
        item.setPosition(Phantom ? -7 :(byte) -11);
        equip.addFromDB(item);
        
        if(Phantom) {
        item = li.getEquipById(1362000);
        item.setPosition((byte) -11);
        equip.addFromDB(item);
        }
        if (shield > 0) {
            item = li.getEquipById(shield);
            item.setPosition((byte) -10);
            equip.addFromDB(item);
        }
        
        if(Demon) {
        item = li.getEquipById(1099001);
        item.setPosition((byte) -10);
        equip.addFromDB(item);
        }
        
        newchar.getInventory(MapleInventoryType.USE).addItem(new Item(2000013, (byte) 0, (short) 100, (byte) 0));
        newchar.getInventory(MapleInventoryType.USE).addItem(new Item(2000014, (byte) 0, (short) 100, (byte) 0));
        
        if (MapleCharacterUtil.canCreateChar(name, c.isGm()) && (!LoginInformationProvider.getInstance().isForbiddenName(name) || c.isGm()) && (c.isGm() || c.canMakeCharacter(c.getWorld()))) {
            MapleCharacter.saveNewCharToDB(newchar, Job, db);
            c.getSession().write(CharlistRequest.addNewCharEntry(newchar, true));
            c.createdChar(newchar.getId());
        } else {
            c.getSession().write(CharlistRequest.addNewCharEntry(newchar, false));
        }
        
        System.out.println("------------------------------------------");
        System.out.println("Name : "+name);
        System.out.println("Job : "+Job);
        System.out.println("DB : "+db);
        System.out.println("Gender : "+gender);
        System.out.println("SkinColor : "+skinColor);
        System.out.println("Face : "+face);
        System.out.println("Hair : "+hair);
        System.out.println("Mark : "+Mark);
        System.out.println("TOP : "+top);
        System.out.println("Bottom : "+bottom);
        System.out.println("Shoes : "+shoes);
        System.out.println("Weapon : "+weapon);
        System.out.println("Shield :"+shield);
        System.out.println("------------------------------------------");
    }
}
